Gives back the place (the index; 0 is the first place) of the first place in target that holds a value equal to value, or -1 if value is not in target at all. If you only want to know yes or no, array_contains does that.
array_find(target, value) -> int| Name | Type |
|---|---|
target | array |
value | element |
int
Gives back the place of the first place in target, an array, that holds a value equal to value. Places are counted from 0, so the first place is 0, the second is 1, and so on.
If value is not in the array at all, you get -1. There is no place -1, so it can never be mistaken for a real answer: -1 always means "not there".
var at = array_find(doors, 8); // 1, if doors holds 4, 8, 15
var no = array_find(doors, 99); // -1, because 99 is not in the arrayIf you only want to know yes or no, and not where, array_contains answers that with true or false.
A place number is never below 0, so a simple test tells you whether the search found something:
var doors = array_create(3, 0);
doors[1] = 8;
var at = array_find(doors, 8);
if (at >= 0) {
doors[at] = 0; // put the 8 back to 0
}Do not use the answer as a place before you have checked it. Reading doors[-1] is a mistake: the Game Boy Advance module, which the game is built on, checks every index and stops the game with an "Invalid index" message. A miss gives -1, so a search that finds nothing and goes straight to doors[at] ends the game.
The search starts at the front and stops at the first match. If value is in the array twice, you get the place of the one nearer the front. To find the next one, write a for loop over the places with array_length.
The same as array_contains, because they use the same search: numbers are equal when they are the same number, and text is equal when every letter is the same. So array_find(items, v) >= 0 is true exactly when array_contains(items, v) is true.
| You do this | What happens |
|---|---|
| search for a value that is not there | -1 |
| search for a value that is there more than once | the place of the first one |
| give it a list | refused when checked. A list has its own, list_find |
| use the answer as a place without checking it | if it was -1, the game stops with "Invalid index" |
array_find only looks. The array is exactly the same afterwards.
A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_find inside a script searches the caller's array, whatever its size.
Nothing is set aside. It looks at one place after another from the front until it finds a match, so a longer array takes longer, and a search that finds nothing looks at every place. No timing was measured.
var doors = array_create(3, 0);
doors[0] = 4;
doors[1] = 8;
doors[2] = 15;
var at8 = array_find(doors, 8); // 1
var at99 = array_find(doors, 99); // -1 - 99 is not in the arrayA plain left-to-right search (find, the GBA module's own alias for std::find, the same call the contains function makes) that stops at the first match and counts how far it went - O(n) in how many items target holds, same cost as writing the loop by hand. It compares with ==, exactly like the contains function, so the answer is -1 when and only when the contains function says false. -1 is not a place, and target[-1] stops the game with "Invalid index" like any other bad index does - test the answer with >= 0 before using it as one.